package com.base.mybaseproject.demo.Map;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

import com.support.Utils;

public class MusicService extends Service {

	private MusicBind musicBind = new MusicBind();
	private MediaPlayer mp;
	private String TAG = "MusicService";

	public class MusicBind extends Binder {
		MusicService getService() {
			return MusicService.this;
		}
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
	}

	@Override
	public void onDestroy() {
		if (mp != null) {
			mp.stop();
			mp.release();
			mp.reset();
		}
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {

		return musicBind;
	}

	@Override
	public boolean onUnbind(Intent intent) {

		if (mp != null) {
			mp.stop();
			mp.release();
			mp.reset();
		}
		return super.onUnbind(intent);
	}

	public void play(int musicId) {
		if (mp == null) {
			mp = MediaPlayer.create(getApplicationContext(), musicId);
		}
		if (!mp.isPlaying())
			mp.start();
		Utils.d(TAG, mp.getCurrentPosition() + "");
	}
}
